package com.intpro.demo.Service;

import com.intpro.demo.DAO.UserToken;
import com.intpro.demo.Utils.DateUtil;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

@Service
public class TokenService
{

	@Autowired
	MongoTemplate mongoTemplate;

		public void updateToke(UserToken t)
	{
		mongoTemplate.save(t);
	}

	public boolean checkToken(long uid, String token)
	{
		Query q1 = new Query(Criteria.where("uid").is(uid));
		UserToken t = mongoTemplate.findOne(q1, UserToken.class);
		if (t == null)
		{
			return false;
		}
		return t.token.equals(token) &&
				DateUtil.timeAdd(3, t.createTime).after(new Date());
	}


}
